package org.xtense.rcp.views;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;
import org.xtense.data.DBConfig;
import org.xtense.data.DBManager;
import org.xtense.engine.actions.QueryAction;
import org.xtense.engine.actions.XAction;
import org.xtense.rcp.editors.ResultViewer;
import org.xtense.rcp.editors.input.ResultViewerInput;
import org.xtense.util.StringUtil;

public class QueryView extends ViewPart {
	public static String View_ID = "XtenseRCP.QueryView";

	private static Combo documentName;
	private Text xpathExpr;
	private Button query;
	private static HashMap<String, Integer> docs;

	@Override
	public void createPartControl(Composite parent) {
		Composite topComp = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(3, false);
		layout.verticalSpacing = 15;
		topComp.setLayout(layout);

		Label nameLabel = new Label(topComp, SWT.NONE);
		nameLabel.setText("Name:");
		documentName = new Combo(topComp, SWT.READ_ONLY);
		GridData nameGdata = new GridData(GridData.FILL_HORIZONTAL);
		nameGdata.horizontalSpan = 2;
		documentName.setLayoutData(nameGdata);

		Label xpathLabel = new Label(topComp, SWT.NONE);
		xpathLabel.setText("Xpath:");
		xpathExpr = new Text(topComp, SWT.BORDER);
		GridData xpathGdata = new GridData(GridData.FILL_HORIZONTAL);
		xpathGdata.horizontalSpan = 2;
		xpathExpr.setLayoutData(xpathGdata);
		
		xpathExpr.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(SelectionEvent e) {
				handleQuerySelected();
			}
		});

		query = new Button(topComp, SWT.NONE);
		query.setText("Execute Query");
		GridData queryGdata = new GridData(GridData.FILL_HORIZONTAL);
		queryGdata.horizontalSpan = 2;
		query.setLayoutData(queryGdata);

		query.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleQuerySelected();
			}

		});

	}

	public static void fillDocNameCombo(HashMap<String, Integer> docs) {
		if (documentName != null) {
			String[] items = docs.keySet().toArray(
					new String[docs.values().size()]);
			documentName.setItems(items);
			documentName.select(0);
			QueryView.docs = docs;
		}
	}
	
	public static void removeDoc(String docName) {
		docs.remove(docName);
		documentName.remove(docName);
	}

	public void handleQuerySelected() {
		String docName = documentName.getText();
		if (!StringUtil.isEmpty(docName)) {
			int id = docs.get(docName);
			String xpath = xpathExpr.getText();
			XAction query = new QueryAction(new DBManager(DBConfig
					.getInstance(false, null)), id);
			IWorkbenchPage workbenchPage = getViewSite().getPage();
			try {
				IEditorPart editor = workbenchPage.openEditor(
						new ResultViewerInput(), ResultViewer.class.getName());
				((ResultViewer) editor).showResult(query.execute(xpath));
			} catch (PartInitException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
